//
//  WordListIPadViewController.m
//  EnglishStudyApp
//
//  Created by Nong Trung Nghia on 10/18/12.
//  Copyright (c) 2012 abc. All rights reserved.
//

#import "WordListIPadViewController.h"
#import "WordDetailIPadViewController.h"
#import "WordListView.h"
#import "WordListViewModel.h"
@interface WordListIPadViewController ()

@end

@implementation WordListIPadViewController
@synthesize detailViewController=_detailViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        self.view.backgroundColor=[UIColor redColor];
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _listView=[[WordListView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, APP_SCREEN_HEIGHT-self.tabBarController.tabBar.frame.size.height-self.navigationController.navigationBar.frame.size.height)];
    
    [_listView setModel:[WordListViewModel currentModel]];
    _listView.delegate=self;
    [self.view addSubview:_listView];
    [_detailViewController setWord:[_listView.model.wordList objectAtIndex:0]];
}
-(void)loadView{
    [super loadView];
    self.title=@"Word List";
    
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
//    [self splitViewController:self.splitViewController shouldHideViewController:self inOrientation:toInterfaceOrientation];
    CGFloat height=(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation==UIInterfaceOrientationLandscapeRight)?[[UIScreen mainScreen] applicationFrame].size.width:[[UIScreen mainScreen] applicationFrame].size.height;
    
    [_listView setFrame:CGRectMake(0, 0, self.view.frame.size.width, height-self.tabBarController.tabBar.frame.size.height-self.navigationController.navigationBar.frame.size.height)];
//    [_listView layoutSubviews];
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        self.detailViewController.navigationItem.leftBarButtonItem=nil;
        }
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - splitviewController delegate
- (void)splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc{

    [barButtonItem setTitle:@"Words"];
    _pvc=pc;
    self.detailViewController.navigationItem.leftBarButtonItem=barButtonItem;
    
}
- (void)splitViewController:(UISplitViewController *)svc
     willShowViewController:(UIViewController *)aViewController
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{
    
}
- (void)splitViewController:(UISplitViewController *)svc
          popoverController:(UIPopoverController *)pc
  willPresentViewController:(UIViewController *)aViewController{
//    aViewController=self;
}
//
- (BOOL)splitViewController:(UISplitViewController *)svc
   shouldHideViewController:(UIViewController *)vc
              inOrientation:(UIInterfaceOrientation)orientation {
    if(orientation==UIInterfaceOrientationPortrait||orientation==UIInterfaceOrientationPortraitUpsideDown){
        return YES;
    }
    _detailViewController.navigationItem.leftBarButtonItem=nil;
    return NO;
    
}
#pragma mark - rotate
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(3_0){
    
}

#pragma mark - Word List View Delegate
-(void)wordListView:(WordListView*)aWordsListView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    NSLog(@"%d",indexPath.row);
    [_detailViewController setWord:[aWordsListView.model.wordList objectAtIndex:indexPath.row]];
    [_pvc dismissPopoverAnimated:YES];
    
}



@end
